
import java.awt.*;
import java.awt.event.*;
import java.util.Timer;
import java.util.*;

import javax.swing.*;


public class ManagerDefaultFrame extends JFrame{
    public ManagerDefaultFrame(String userid){


        SQLOperation sql=new SQLOperation();

        setTitle("System Management Operation");
        setLayout(null);




        JLabel title=new JLabel("  SmartHome Management System");
        title.setFont(new Font("",Font.BOLD,60));
        title.setBounds(0,0,1000,125);
        add(title);


        JButton logout=new JButton("Log out");
        logout.setFont(new Font("",Font.BOLD,25));

        logout.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int exitOperation=JOptionPane.showConfirmDialog
                        (null, "Confirm log out?","Confirm",JOptionPane.YES_NO_OPTION);

                if(exitOperation==JOptionPane.YES_OPTION){
                    setVisible(false);
                    new Login();
                }

            }
        });


        logout.setBounds(1030,25,140,75);
        add(logout);



        JButton familyOp=new JButton("Family Operation");
        familyOp.setFont(new Font("",Font.BOLD,25));
        familyOp.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                new Family(userid);
            }
        });
        familyOp.setBounds(30,150,340,75);
        add(familyOp);


        JButton userOp=new JButton("User Operation");
        userOp.setFont(new Font("",Font.BOLD,25));
        userOp.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                new User(userid);

            }
        });
        userOp.setBounds(30,275,340,75);
        add(userOp);


        JButton equipOp=new JButton("Equipment Operation");
        equipOp.setFont(new Font("",Font.BOLD,25));
        equipOp.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                new Equip(userid);
            }
        });
        equipOp.setBounds(30,400,340,75);
        add(equipOp);


        JButton classOp=new JButton("Equip Class Operation");
        classOp.setFont(new Font("",Font.BOLD,25));
        classOp.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                new Class(userid);

            }
        });
        classOp.setBounds(30,525,340,75);
        add(classOp);


        JButton dataOp=new JButton("Data Operation");
        dataOp.setFont(new Font("",Font.BOLD,25));
        dataOp.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                new Data(userid);
            }
        });
        dataOp.setBounds(30,650,340,75);
        add(dataOp);


        //alarm operation
/*
        Object alarmHouse="";
        alarm.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog
                      (null, "There is an abnormal alarm happened at"+alarmHouse,
                              "Abnormal Alarm",JOptionPane.ERROR_MESSAGE);


                }
            }
        });

*/


        String[] userAttributes={"uUID","phoneNum","gender"};
        String[] accountAttributes={"aName"};
        String[] attributes={"userUID","phoneNum","gender","userName"};
        Object[][] userDB=sql.search("User",userAttributes,"shanchu",1);

        Object[][] userList=new Object[userDB.length][4];
         for(int i=0;i< userDB.length;i++){
             for(int j=0;j<3;j++) userList[i][j]=userDB[i][j];
            // System.out.println(userDB[i][0]);
             userList[i][3]=sql.search("Account",accountAttributes,"UID",userDB[i][0])[0][0];
        }


        JTable userTable=new JTable(userList,attributes);
        userTable.setRowHeight(30);
        userTable.setFont(new Font("",Font.PLAIN,15));

        userTable.setEnabled(true);
        userTable.getTableHeader().setFont(new Font("",Font.BOLD,20));
        JScrollPane scrollPane=new JScrollPane(userTable);
        scrollPane.setBounds(400,125,770,750);
        add(scrollPane);

/*


        Timer  timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                String[] alarmAttri={"alarmType","Device_idDevice","alarmTriggered","idInfo"};
                Object[][] alarm;
                alarm=sql.search("Alarm",alarmAttri,"alarm",1);
                Object searchFamilyID[][];
                String[] FamilyAttri={"Family_familyID"};
                for(int i=0;i<alarm.length;i++){
                    if((int)(alarm[i][2])==1){

                        searchFamilyID=sql.search("Device",FamilyAttri,"idDevice",alarm[i][1]);


                        sql.modify("Alarm","idInfo",alarm[i][3],"alarmTriggered",0);
                        JOptionPane.showMessageDialog
                                (null, alarm[i][0]+" Alarm happened in "+searchFamilyID[0][0]+"!",
                                        alarm[i][0]+" Alarm",JOptionPane.ERROR_MESSAGE);




                    }

                }

            }
        }, 2000, 40);


*/
        setSize(1200,800);
        setResizable(false);
        setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        setVisible(true);
    }
}
